using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// 文件重名检索工具
/// </summary>
[InlineEditor]
public class TextComponentCheckTool : BearScriptableObject
{
    [FolderPath]
    public List<string> searchPaths = new List<string>();
    

    [TableList] 
    public Dictionary<GameObject, List<string>> records = new Dictionary<GameObject, List<string>>();
    
    /// <summary>
    /// 固定生成路径
    /// </summary>
    public override string DefaultPath
    {
        get => "Assets/TextComponentCheckTool.asset";
    }
    
    /// <summary>
    /// 单例对象，快速获取
    /// </summary>
    private static TextComponentCheckTool _instance;
    public static TextComponentCheckTool Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = Init();
            }

            return _instance;
        }
    }

    // 用于Tools 手动创建对象，前期测试使用
    [MenuItem("Tools/BearTool/Text Component Check")]
    private static void menuOption()
    {
        generate<TextComponentCheckTool>();
    }

    /// <summary>
    /// 初始化，新工具可以按照这个模板来重新写
    /// </summary>
    /// <returns></returns>
    public static TextComponentCheckTool Init()
    {
        var instance = CreateInstance<TextComponentCheckTool>();
        instance = AssetDatabase.LoadAssetAtPath<TextComponentCheckTool>(instance.DefaultPath);
        if (instance == null)
        {
            menuOption();
            instance = AssetDatabase.LoadAssetAtPath<TextComponentCheckTool>(instance.DefaultPath);
        }

        _instance = instance;

        return instance;
    }

    [Button("检查 Prefab Comp", ButtonSizes.Large)]
    private void ForTest()
    {
        CheckTextComponent();
    }

    /// <summary>
    /// 指定路径检索 Text Component
    /// </summary>
    public void CheckTextComponent()
    {
        if (records == null)
            records = new Dictionary<GameObject, List<string>>();
        records.Clear();
        
        foreach (var folderPath in searchPaths)
        {
            var files = Directory.GetFiles(folderPath.RelaToFullPath(), "*.*", SearchOption.AllDirectories)
                .Where(file => Path.GetExtension(file).Equals(".prefab"));

            foreach (var file in files)
            {
                var prefabPath = file.FullPathToRela();
                var obj = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
                var names = checkChildrenComp(obj.transform);
                if (names.Count <= 0)
                    continue;
                records.Add(obj, names);
            }
        }
    }

    /// <summary>
    /// 检查子节点
    /// </summary>
    /// <returns></returns>
    private List<string> checkChildrenComp(Transform tran)
    {
        List<string> names = new List<string>();

        for (int i = 0; i < tran.childCount; i++)
        {
            var child = tran.GetChild(i);
            var txt = child.GetComponent<Text>();
            if (txt != null)
                names.Add(child.name);

            names = names.Concat(checkChildrenComp(child)).ToList<string>();
        }

        return names;
    }
}

